<template>
	<view>
		<view class="goods-lists">
			<view v-for="(item,index) in goodslist" :key = "index" @click="gotoGoodsDetail(item)">
				<my-goods :goods = "item"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj:{ //请求参数对象
					query:"",
					cid:"",
					pagenum:1,
					pagesize:10,
				},
				goodslist:[],
				total:0,//总条数
				pagenum:0,//当前页数
				isOpen:true,//节流阀
			};
		},
		onLoad(option) {
			this.queryObj.query = option.query || '';
			this.queryObj.cid = option.cid || '';
			console.log('queryObj',this.queryObj);
			this.getGoodsList();
		},
		onReachBottom(){
			console.log('用户拉了');
			if(this.queryObj.pagenum*this.queryObj.pagesize >= this.total) return uni.$showMsg('没有更多了')
			this.queryObj.pagenum += 1;
			this.getGoodsList();
		},
		// 下拉刷新事件
		onPullDownRefresh(){
			console.log('用户下拉刷新')
			// 先重置参数
			this.queryObj.pagenum = 1;
			this.total = 0;
			this.isOpen = false;
			this.goodslist = [];
			// 重新发起请求
			 this.getGoodsList(() => uni.stopPullDownRefresh())
		},
		methods:{
			// 点击列表跳转到goods_datail页面
			gotoGoodsDetail(item){
				console.log('item',item)
				uni.navigateTo({
					url:`../goods_detail/goods_detail?goods_id=${item.goods_id}`
				})
			},
			async getGoodsList(cb){
				this.isOpen = false;
				const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj);
				// 相当于判断是否存在cb 如果存在就调用cb()，达到关闭下拉刷新的效果
				cb && cb()
				if(res.meta.status !== 200) return uni.$showMsg();
				let arr = res.message.goods;
				
				this.goodslist = [...this.goodslist , ...arr]
				this.total = res.message.total;
				this.pagenum = res.message.pagenum;
				console.log('res==>',res)
				this.isOpen = true;
			}
		}
	}
</script>

<style lang="scss">

</style>
